/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.desktop.kungkhie.node.explorer;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.swing.Action;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.util.actions.SystemAction;
import org.openide.actions.DeleteAction;
import org.openide.actions.PropertiesAction;
import org.openide.util.lookup.Lookups;

/**
 * It was a booboo to name a domain entity as Node.  KungkhieNode would have
 * been preferable.  However, for the present, use fully qualified class names.
 * 
 * @author clayton
 */
public class KungkhieNodeChildFactory extends ChildFactory<com.kungkhies.desktop.library.entities.Node>
{
    private List<com.kungkhies.desktop.library.entities.Node> resultList;

    public KungkhieNodeChildFactory(List<com.kungkhies.desktop.library.entities.Node> resultList)
    {
        this.resultList = resultList;
    }

    @Override
    protected boolean createKeys(List<com.kungkhies.desktop.library.entities.Node> list)
    {
        for (com.kungkhies.desktop.library.entities.Node kungkhieNode : resultList)
        {
            list.add(kungkhieNode);
        }
        return true;
    }

    @Override
    protected org.openide.nodes.Node createNodeForKey(com.kungkhies.desktop.library.entities.Node c) {
        org.openide.nodes.Node node = new AbstractNode(Children.LEAF, Lookups.singleton(c)) {

            @Override
            public Action[] getActions(boolean context) {
                Action[] result = new Action[]{
                    SystemAction.get(DeleteAction.class),
                    SystemAction.get(PropertiesAction.class)
                };
                return result;
            }

            @Override
            public boolean canDestroy() {
                com.kungkhies.desktop.library.entities.Node kungkhieNode = this.getLookup().lookup(com.kungkhies.desktop.library.entities.Node.class);
                return kungkhieNode != null;
            }

            @Override
            public void destroy() throws IOException {
                if (deleteActivity(this.getLookup().lookup(com.kungkhies.desktop.library.entities.Node.class).getNodeId())) {
                    super.destroy();
                    KungkhieNodeExplorerTopComponent.refreshNode();
                }
            }

        };
        node.setDisplayName(c.getDisplayName());
        node.setShortDescription(c.getNodeType());
        return node;
    }

    private static boolean deleteActivity(long kungkhieNodeId) {
        EntityManager entityManager = Persistence.createEntityManagerFactory("KungkhieEntityLibraryPU").createEntityManager();
        entityManager.getTransaction().begin();
        try {
            com.kungkhies.desktop.library.entities.Node toDelete = entityManager.find(com.kungkhies.desktop.library.entities.Node.class, kungkhieNodeId);
            entityManager.remove(toDelete);
            // so far so good
            entityManager.getTransaction().commit();
        } catch(Exception e) {
            Logger.getLogger(KungkhieNodeChildFactory.class.getName()).log(
                    Level.WARNING, "Cannot delete a kungkhie node with id {0}, cause: {1}", new Object[]{kungkhieNodeId, e});
            entityManager.getTransaction().rollback();
        }
        return true;
    }
}
